package com.cvmars.jianyou.module.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.api.model.UserMode;
import com.cvmars.jianyou.model.UserInfoManager;
import com.cvmars.jianyou.module.base.BaseActivity;
import com.cvmars.jianyou.utils.KeyBoradUtils;
import com.cvmars.jianyou.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

public class LoginPassWordActivity extends BaseActivity {

    @BindView(R.id.txt_login_register)
    TextView txtLoginRegister;
    @BindView(R.id.txt_login_reset)
    TextView txtLoginReset;
    @BindView(R.id.txt_accouont)
    EditText txtAccouont;
    @BindView(R.id.txt_password)
    EditText txtPassword;
    @BindView(R.id.txt_login_btn)
    Button txtLoginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_pass_word);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.txt_login_register, R.id.txt_login_reset})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_login_register:
                startActivity(new Intent(this, RegisterActivity.class));
                finish();
                break;
            case R.id.txt_login_reset:
                startActivity(new Intent(this, ResetPasswordActivity.class));
                break;
        }
    }


    private void onLogin() {

        String phone = txtAccouont.getText().toString();
        String password = txtPassword.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.show("手机号不能为空");
            return;
        }

        if (phone.length() != 11) {
            ToastUtils.show("手机号格式不正确");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.show("密码不能为空");
            return;
        }

        KeyBoradUtils.closeKeyborad(this);

        getLoadDialogAndShow();
        HttpUtils.getInstance().toSubscribe(Api.getInstance().postLogin(phone, password)
                , new SimpleSubscriber<HttpResult<UserMode>>() {
                    @Override
                    protected void _onNext(HttpResult<UserMode> o) {

                        getLoadDialogAndDismiss();
                        ToastUtils.show("登录成功");
                        UserInfoManager.getInstance().onLogin(o.getData());
                        startActivity(new Intent(LoginPassWordActivity.this, HomeActivity.class));
                        finish();
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }

    @OnClick(R.id.txt_login_btn)
    public void onViewClicked() {

        onLogin();
    }


}
